package cn.rui.service.impl;

import cn.rui.entity.PageResult;
import cn.rui.entity.QueryPageBean;
import cn.rui.mapper.RoleMapper;
import cn.rui.pojo.Role;
import cn.rui.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徽州大都督
 * @date 2020/9/2
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询全部角色信息
     *
     * @return
     */
    public List<Role> findAllRoleName() {

        return roleMapper.findAll ();
    }

    /**
     * 分页查询
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage ();
        Integer pageSize = queryPageBean.getPageSize ();
        String queryString = queryPageBean.getQueryString ();
        PageHelper.startPage (currentPage, pageSize);

        Page<Role> page = roleMapper.findPage (queryString);
        List<Role> roles = page.getResult ();
        long total = page.getTotal ();
        return new PageResult (total, roles);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public Role findById(Integer id) {
        return roleMapper.findById (id);
    }

    /**
     * 根据角色id查询关联的执行权限id集合
     */
    public List<Integer> findPermissionsByRoleId(Integer id) {

        return roleMapper.findPermissionsByRoleId(id);
    }

    /**
     * 根据角色id查询关联的菜单id集合
     * @param id
     * @return
     */
    public List<Integer> findUrlsByRoleId(Integer id) {

        return roleMapper.findUrlsByRoleId(id);
    }

    /**
     * 添加角色
     * @param menuIds
     * @param permissionIds
     * @param role
     */
    public void add(List<Integer> menuIds, List<Integer> permissionIds, Role role) {
        roleMapper.add(role);

        Integer roleId = role.getId ();

        //添加角色和权限关联
        for (Integer permissionId : permissionIds) {
            Map<String,Object> map = new HashMap<> ();
            map.put ("role_id",roleId);
            map.put ("permission_id",permissionId);
            roleMapper.addRoleAndPermission(map);
        }

        //添加角色和菜单关联
        for (Integer menuId : menuIds) {
            Map<String,Object> map = new HashMap<> ();
            map.put ("role_id",roleId);
            map.put ("menu_id",menuId);
            roleMapper.addRoleAndMenu (map);
        }
    }

    /**
     * 编辑角色
     * @param menuIds
     * @param permissionIds
     * @param role
     */
    public void edit(List<Integer> menuIds, List<Integer> permissionIds, Role role) {

        //修改角色信息
        roleMapper.edit (role);

        //删除角色关联的权限信息，并添加信息的信息
        roleMapper.deletePermissionAndRoleByRoleId(role.getId ());

        //删除角色关联的菜单信息，并添加新的信息
        roleMapper.deleteMenuAndRoleByRoleId (role.getId ());


        //添加角色和权限关联
        for (Integer permissionId : permissionIds) {
            Map<String,Object> map = new HashMap<> ();
            map.put ("role_id",role.getId ());
            map.put ("permission_id",permissionId);
            roleMapper.addRoleAndPermission(map);
        }

        //添加角色和菜单关联
        for (Integer menuId : menuIds) {
            Map<String,Object> map = new HashMap<> ();
            map.put ("role_id",role.getId ());
            map.put ("menu_id",menuId);
            roleMapper.addRoleAndMenu (map);
        }
    }

    /**
     * 根据id删除角色
     * @param id
     */
    public void delete(Integer id) {
        //删除角色关联的权限
        roleMapper.deletePermissionAndRoleByRoleId (id);

        //删除角色关联的菜单
        roleMapper.deleteMenuAndRoleByRoleId (id);

        //删除角色
        roleMapper.deleteById(id);
    }
}
